package com.reactim.message.protection;

import lombok.Builder;
import lombok.Data;

import java.util.Map;

/**
 * 资源使用统计
 */
@Data
@Builder
public class ResourceUsageStats {
    
    /**
     * 全局连接数
     */
    private final int globalConnectionCount;
    
    /**
     * 用户连接数统计
     */
    private final Map<Long, Integer> userConnectionCounts;
    
    /**
     * IP连接数统计
     */
    private final Map<String, Integer> ipConnectionCounts;
    
    /**
     * 总创建连接数
     */
    private final long totalConnectionsCreated;
    
    /**
     * 总拒绝连接数
     */
    private final long totalConnectionsRejected;
    
    /**
     * 总限流请求数
     */
    private final long totalRequestsRateLimited;
    
    /**
     * 活跃限流器数量
     */
    private final int activeRateLimiters;
    
    /**
     * 获取连接成功率
     */
    public double getConnectionSuccessRate() {
        long totalAttempts = totalConnectionsCreated + totalConnectionsRejected;
        if (totalAttempts == 0) {
            return 1.0;
        }
        return (double) totalConnectionsCreated / totalAttempts;
    }
    
    /**
     * 获取用户连接数最多的用户
     */
    public Long getTopUserByConnections() {
        return userConnectionCounts.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);
    }
    
    /**
     * 获取IP连接数最多的IP
     */
    public String getTopIpByConnections() {
        return ipConnectionCounts.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);
    }
    
    /**
     * 获取平均每用户连接数
     */
    public double getAverageConnectionsPerUser() {
        if (userConnectionCounts.isEmpty()) {
            return 0.0;
        }
        return userConnectionCounts.values().stream()
                .mapToInt(Integer::intValue)
                .average()
                .orElse(0.0);
    }
    
    /**
     * 获取平均每IP连接数
     */
    public double getAverageConnectionsPerIp() {
        if (ipConnectionCounts.isEmpty()) {
            return 0.0;
        }
        return ipConnectionCounts.values().stream()
                .mapToInt(Integer::intValue)
                .average()
                .orElse(0.0);
    }
    
    /**
     * 获取活跃用户数
     */
    public int getActiveUserCount() {
        return userConnectionCounts.size();
    }
    
    /**
     * 获取活跃IP数
     */
    public int getActiveIpCount() {
        return ipConnectionCounts.size();
    }
    
    /**
     * 获取连接拒绝率
     */
    public double getConnectionRejectionRate() {
        long totalAttempts = totalConnectionsCreated + totalConnectionsRejected;
        if (totalAttempts == 0) {
            return 0.0;
        }
        return (double) totalConnectionsRejected / totalAttempts;
    }
    
    /**
     * 获取最大用户连接数
     */
    public int getMaxUserConnections() {
        return userConnectionCounts.values().stream()
                .mapToInt(Integer::intValue)
                .max()
                .orElse(0);
    }
    
    /**
     * 获取最大IP连接数
     */
    public int getMaxIpConnections() {
        return ipConnectionCounts.values().stream()
                .mapToInt(Integer::intValue)
                .max()
                .orElse(0);
    }
}